/**
 * Created by lizhe on 2018/4/16.
 */
var urladdr = "/smart-campus/";
var articleAddr = "http://192.168.201.100:8081/smart-campus/";
var imgUrl = "http://192.168.201.100:8082/";
var Mytoken = localStorage.getItem("token");
//var Mytoken = "25251101ac32e1b9bd396ef3ec31c89f";
$(document).ready(function(){
    $("#modal_confirm").click(function(){
        modalclose('#infomodal');
    });

    $("#img_confirm").click(function(){
        uplodbase64();
    });
});

function showmodal(name){
    $(name).modal({backdrop: "static", keyboard: false});
    centerModals(name);
}

function modalclose(name){
    $(name).modal('hide');
}
function centerModals(name) {
    var $clone = $(name).clone().css('display', 'block').appendTo('body'); var top = Math.round(($clone.height() - $clone.find('.modal-content').height()) / 2);
    top = top > 0 ? top : 0;
    $clone.remove();
    $(name).find('.modal-content').css("margin-top", top);
}

function modalinfo(info){
    $("#infomodal").modal("show");
    centerModals("#infomodal");
    document.getElementById("info").innerHTML = info;
}

//时间戳改时间
function getdate(time) {
    var now = new Date(time),
        y = now.getFullYear(),
        m = now.getMonth() + 1,
        d = now.getDate();
    return y + "-" + (m < 10 ? "0" + m : m) + "-" + (d < 10 ? "0" + d : d) + " " + now.toTimeString().substr(0, 8);
}

function getDay(time){
    var now = new Date(time),
        y = now.getFullYear(),
        m = now.getMonth() + 1,
        d = now.getDate();
    return y + "-" + (m < 10 ? "0" + m : m) + "-" + (d < 10 ? "0" + d : d);
}

function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
    return currentdate;
}

//单击表单
function clicktable(table){
    var trs = document.getElementById(table).getElementsByTagName('tr');
    var t = document.getElementById(table);
    for( var i=0; i<trs.length; i++ ){
        trs[i].onmousedown = function(){
            tronmousedown(this,table);
        }
    }
}

function tronmousedown(obj,table){
    var trs = document.getElementById(table).getElementsByTagName('tr');
    for( var o=0; o<trs.length; o++ ) {
        if (trs[o] == obj) {
            trs[o].style.background = '#dff0d8';
            trs[o].style.color = '#000000';
        }
        else {
            trs[o].style.backgroundColor = '';
            trs[o].style.color = '#000000';
        }
    }
}

function onprogress(evt){
    var loaded = evt.loaded;     //已经上传大小情况
    var tot = evt.total;      //附件总大小
    var per = Math.floor(100*loaded/tot);  //已经上传的百分比
    $("#progress-div").html( per +"%" );
    $("#progress-div").css("width" , per +"%");
}

function imageClick(id){
    $("#"+id).click();
    var file = document.getElementById(id);
    if (file.outerHTML) {
        file.outerHTML = file.outerHTML;
    } else { // FF(包括3.5)
        file.value = "";
    }
    document.getElementById(id).focus();
}

function changeimage(obj,image){
    var file = obj.files[0];
    if(file!=undefined){
        var type = file.type;
        type = type.split("/");
        type = type[0];
        if(type == "image"){
            showmodal("#imagemodal");
            var id = obj.id;
            id = id.split("-");
            if(id.length>1){
                document.getElementById("imgId").innerHTML = id[1];
            }
            var $image = $('#image');
            var imgurl = window.URL.createObjectURL(obj.files[0]);
            document.getElementById(image).src = imgurl;
            $image.cropper('replace', imgurl);
        }else{
            modalinfo("只能上传图片！");
        }
    }
}

function getRoundedCanvas(sourceCanvas) {
    var canvas = document.createElement('canvas');
    var context = canvas.getContext('2d');
    var width = sourceCanvas.width;
    var height = sourceCanvas.height;

    canvas.width = width;
    canvas.height = height;
    context.beginPath();
    context.arc(width / 2, height / 2, Math.min(width, height) / 2, 0, 2 * Math.PI);
    context.strokeStyle = 'rgba(0,0,0,0)';
    context.stroke();
    context.clip();
    context.drawImage(sourceCanvas, 0, 0, width, height);

    return canvas;
}


function getTime(time){
    var date = new Date(time.replace(/-/g, '/'));
    var time1 = date.getTime();
    return time1;
}

function drawToCanvas(imgData){
    var cvs = document.querySelector('#cvs');
    cvs.width=300;
    cvs.height=400;
    var ctx = cvs.getContext('2d');
    var img = new Image;
    img.src = imgData;
    img.onload = function(){//必须onload之后再画
        ctx.drawImage(img,0,0,300,400);
        strDataURI = cvs.toDataURL();//获取canvas base64数据
    }
}